package DTaskExportApp::Controller::Export;
use Mojo::Base 'Mojolicious::Controller';
use 5.018;
use warnings;

use Mango;
my $mango = Mango->new;
use Mojo::JSON qw(true false);
use DTask::Utils qw(renderJsonResponse);
use DTask::Link;
use BugzillaJsonRpc::Bug;
use Data::Dumper;
use Tower::API;

sub bugzillaBugToTowerTodo
{
	my $c = shift;
	my $bug_id = $c->stash('bug_id');

	my $headers = $c->tx->req->headers;
	my $todolist_guid = $c->stash('todolist_guid');
	my $tower_token = $headers->header('Tower-Token');

	DTask::Link::setMango($mango);

	DTask::Link::getLinks('bugzilla', $bug_id,'tower_todo', sub {
		my ($tower_todos, $err) = @_;	
		if ( defined $tower_todos && @$tower_todos )
		{
			renderJsonResponse($c, $tower_todos);
		}
		else
		{
			_export_bug_to_tower_todo($tower_token, $bug_id, $todolist_guid, sub {
				renderJsonResponse($c, @_);
			});
		}
	});

}

sub _export_bug_to_tower_todo
{
	my ($tower_token, $bug_id, $todolist_guid, $cb) = @_;
	my $delay = Mojo::IOLoop->delay(
		sub {
			my ($delay) = @_;
			my $end = $delay->begin;
			BugzillaJsonRpc::Bug::get($bug_id, sub {
				my ($ret) = @_;
				if ($ret->{error})
				{
					$delay->pass($ret);
					$end->();
				}
				else
				{
					my $bug_info = $ret->{result};
					warn Dumper $bug_info;
					my $title = "#bugzilla #$bug_id ". $bug_info->{summary};
					$delay->pass({
							error => false,
							title => $title,
							todo_comment => "from: <a href=\"https://bugzilla.deepin.io/show_bug.cgi?id=$bug_id\">bugzilla $bug_id</a>"
						});
					$end->();
				}
					
			});
		},
		sub {
			my ($delay, $info) = @_;

			if ($info->{error})
			{
				$delay->pass($info);
				return;
			}
			
			my $end = $delay->begin;
			Tower::API::addTodo($tower_token, $todolist_guid, $info->{title}, undef, undef,sub {
				my ($ret) = @_;
				if ($ret->{error})
				{
					$delay->pass($ret);
					$end->();
				}
				else
				{
					my $result = $ret->{result};
					warn 'add todo done, dump ret result';
					warn Dumper $result;

					my $todo_guid = $result->{guid};
					$info->{todo_guid} = $todo_guid;

					my $project_guid = $result->{project}{guid};
					$info->{todo_url} = "https://tower.im/projects/$project_guid/todos/$todo_guid";

					$info->{bz_comment} = "Export to tower by dtask\n".
						$result->{project}{name}. ' :: ' . $result->{todolist}{name} . "\n". $info->{todo_url};

					DTask::Link::linkWith('tower_todo', $todo_guid, 'bugzilla', $bug_id, sub {
						$delay->pass($info);	
						$end->();
					});

				}
			});
		},
		sub {
			my ($delay, $info) = @_;

			if ($info->{error})
			{
				$delay->pass($info);
				return;
			}

			warn 'step 3 dump $info';
			warn Dumper $info;
			my $end1 = $delay->begin;
			Tower::API::addTodoComment($tower_token, $info->{todo_guid}, $info->{todo_comment}, sub {
				warn 'add todo comment done';	
				$delay->pass($info);
				$end1->();
			});
			BugzillaJsonRpc::Bug::update($bug_id,
				{
					url => $info->{todo_url},
					comment => {body => $info->{bz_comment} }
				}, $delay->begin );
		}
	);

	$delay->on(finish => sub {

		my ($delay, $info) = @_;
		if ($info->{error})
		{
			warn "_export_bug_to_tower_tod: on finish : error : ". $info->{error_message};
			$cb->(undef, $info->{error_message});
		}
		else
		{

			my $todo_url = $info->{todo_url};
			say "_export_bug_to_tower_todo: on finish : todo url is $todo_url";
			$cb->($todo_url);
		}
	});

}
